﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace WindowsGame1
{
    class Bat : Enemy
    {
        private float m_fMovementTimer = 0.0f;
        private float m_fRecoil = 0.0f;
        private bool m_bChasing = false;

        public Bat()
        {


        }

        public Bat(string szFile)
        {

            base.Load(szFile);
        }

        public override void Render(SpriteBatch spriteBatch, Camera cam)
        {
            if (m_fRecoil > 0.0f)
            {
                spriteBatch.Draw(m_Texture,
                                 new Rectangle(((int)m_Position.X) - (int)cam.m_Position.X, ((int)m_Position.Y) - (int)cam.m_Position.Y, (int)m_EnemySize.X, (int)m_EnemySize.Y),
                                 new Rectangle(0, (int)m_EnemySize.Y, (int)m_EnemySize.X, (int)m_EnemySize.Y),
                                 Color.White,
                                 0.0f,
                                 new Vector2(m_EnemySize.X / 2, m_EnemySize.Y),
                                 m_Direction,
                                 0.0f);
            }
            else
            {

                spriteBatch.Draw(m_Texture,
                                 new Rectangle(((int)m_Position.X) - (int)cam.m_Position.X, ((int)m_Position.Y) - (int)cam.m_Position.Y, (int)m_EnemySize.X, (int)m_EnemySize.Y),
                                 new Rectangle((int)(m_EnemySize.X + ((int)m_fFrame) * m_EnemySize.X), 0, (int)m_EnemySize.X, (int)m_EnemySize.Y),
                                 Color.White,
                                 0.0f,
                                 new Vector2(m_EnemySize.X / 2, m_EnemySize.Y),
                                 m_Direction,
                                 0.0f);

            }
            
            
        }

        public override void Update(float deltaT, KeyboardState LastKeyState)
        {
            Player Target = (ObjectManager.Instance.GetPlayer());



            if (m_fRecoil == 0.0f) 
            {
                m_nMaxFrame = 3;
                m_Direction = (m_Velocity.X > 0.0f ? SpriteEffects.None : SpriteEffects.FlipHorizontally);
                if (m_bChasing == true)
                {
                    Vector2 direction = (Target.m_Position - m_Position + new Vector2(0.0f, -64.0f));
                    direction.Normalize();
                    m_Velocity.Normalize();
                    m_Velocity += direction * 10.0f;
                } 
                else if ((m_Position - Target.m_Position).Length() < 300 && Math.Sign(Target.m_Position.X - m_Position.X) == Math.Sign(m_Velocity.X))
                {
                    Vector2 direction = (Target.m_Position - m_Position + new Vector2(0.0f, -64.0f));
                    direction.Normalize();
                    m_Velocity = direction*10.0f;
                    m_bChasing = true;
                }
                else
                {
                    if (m_fMovementTimer <= 0.0f)
                    {
                        if (GlobalContentManager.Instance.RandomFloat(0.0f, 100.0f) < 40.0f)
                        {
                            m_Velocity.X = GlobalContentManager.Instance.RandomFloat(3.0f, 5.0f)* Math.Sign(GlobalContentManager.Instance.RandomInt(-1,0));
                            m_fMovementTimer = GlobalContentManager.Instance.RandomFloat(100.0f, 200.0f);
                        }
                        else
                        {
                            m_Velocity.X = GlobalContentManager.Instance.RandomFloat(3.0f, 5.0f) * Math.Sign(GlobalContentManager.Instance.RandomInt(-1, 0));
                            m_fMovementTimer = GlobalContentManager.Instance.RandomFloat(10.0f, 20.0f);
                        }
                    }
                }
            }

            m_Position.Y += m_Velocity.Y * deltaT;
            m_Position.X += m_Velocity.X * deltaT;

            if (m_fRecoil > 0.0f)
                m_fRecoil -= deltaT;

            if (m_fRecoil < 0.0f)
                m_fRecoil = 0.0f;

            m_fMovementTimer -= deltaT;
            m_fFrame += deltaT * ((m_Velocity.X > 0.0f) ? m_Velocity.X : -m_Velocity.X) / 8.0f;

            if (m_fFrame >= m_nMaxFrame)
                m_fFrame = 0;

            m_bIsOnGround = false;

        }

        public override void CollisionResponse(BaseObject other)
        {
            if (other.m_ObjectType == OBJ_TYPE.OBJ_PLAYER && m_fRecoil == 0.0f)
            {
                if (((Player)other).m_bAttacking == true && ((!((Player)other).m_bDucking && other.m_fFrame > 2) || (((Player)other).m_bDucking)))
                {
                    Rectangle weaponRect = new Rectangle((int)other.m_Position.X + (other.m_Direction == SpriteEffects.FlipHorizontally ? -84 : 0), (int)other.m_Position.Y - 128, 84, 100);
                    Rectangle enemyRect = new Rectangle((int)m_Position.X - (int)m_CollisionSize.X/2, (int)m_Position.Y - (int)m_CollisionSize.Y, (int)m_CollisionSize.X, (int)m_CollisionSize.Y);


                    if (weaponRect.Intersects(enemyRect))
                    {
                        m_bChasing = true;
                        if (GlobalContentManager.Instance.PlayerStat("Strength") > m_nDef)
                            m_nHp -= GlobalContentManager.Instance.PlayerStat("Strength") - m_nDef;
                        else
                            m_nHp -= 1;

                        if (m_nHp <= 0)
                            m_bIsAlive = false;
                        m_Velocity.X = (other.m_Direction == SpriteEffects.FlipHorizontally ? -16 : 16);
                        m_fRecoil = 2.0f;
                        if (other.m_Direction == SpriteEffects.FlipHorizontally)
                            m_Direction = SpriteEffects.None;
                        else
                            m_Direction = SpriteEffects.FlipHorizontally;
                    }
                }
            }
            else if (other.m_ObjectType == OBJ_TYPE.OBJ_TILE)
            {

                if (!LevelManager.Instance.m_TileProperties[((Tile)other).m_nTileID].m_bIsSolid)
                    return;

                Rectangle myFeet = new Rectangle((int)m_Position.X - (int)m_CollisionSize.X / 4, (int)m_Position.Y - (int)m_CollisionSize.Y / 4, (int)m_CollisionSize.X / 2, (int)m_CollisionSize.Y / 4);
                Rectangle myRect = new Rectangle((int)m_Position.X - (int)m_CollisionSize.X / 2, (int)m_Position.Y - (int)m_CollisionSize.Y, (int)m_CollisionSize.X, (int)m_CollisionSize.Y - (int)m_CollisionSize.Y / 4);
                Rectangle tileRect = new Rectangle((int)other.m_Position.X, (int)other.m_Position.Y, (int)LevelManager.Instance.m_TileSize.X, (int)LevelManager.Instance.m_TileSize.Y);
                Rectangle myLeft = new Rectangle((int)m_Position.X - (int)m_CollisionSize.X / 2 - 4, (int)m_Position.Y - (int)m_CollisionSize.Y / 4, 2, (int)m_CollisionSize.Y / 4);
                Rectangle myRight = new Rectangle((int)m_Position.X + (int)m_CollisionSize.X / 2, (int)m_Position.Y - (int)m_CollisionSize.Y / 4, 4, (int)m_CollisionSize.Y / 4);


                if (myFeet.Intersects(tileRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myFeet, tileRect);
                    if (m_Velocity.Y >= 0.0)
                    {
                        m_Position.Y -= (collRect.Height - 1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                        m_Velocity.Y = 0.0f;
                        m_bIsOnGround = true;
                    }
                    if (collRect.Width < LevelManager.Instance.m_TileSize.X)
                    {
                        //m_Velocity.X *= -1;
                    }
                }
                else if (myRect.Intersects(tileRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myRect, tileRect);
                    Vector2 Angle = new Vector2(myRect.Center.Y - tileRect.Center.Y, myRect.X - tileRect.X);
                    if (collRect.Width > collRect.Height)
                    {
                        m_Position.Y += (collRect.Height + 1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                        if (m_Velocity.Y < 0.0f && m_fRecoil == 0.0f)
                            m_Velocity.Y = 0.0f;
                    }
                    else
                    {
                        m_Position.X += (collRect.Width + 1) * Math.Sign(m_Position.X - other.m_Position.X);
                    }
                }
            }
        }

    }
}
